type MAP = number[][];
import { createMatrix } from "game-algorithm";
import { Hex } from "./Color";
class Tile {
    sprite: Laya.Sprite;
    x: number;
    y: number;
    width: number;
    last: string = "";
    constructor(sprite: Laya.Sprite, x: number, y: number, width: number) {
        this.sprite = sprite;
        this.x = x * width;
        this.y = y * width;
        this.width = width;
    }
    reprint(colorIndex: number) {
        const c = Hex[colorIndex];
        // console.log(c);
        if (this.last !== c) {
            this.last = c;
            this.sprite.graphics.drawRect(
                this.x,
                this.y,
                this.width,
                this.width,
                c
            );
        }
    }
}
export default class MiniMap {
    radius: number;
    maps: Tile[][];
    matrix: number[][];
    miniMap: Laya.Sprite;
    constructor(father: Laya.Sprite, map: MAP, radius: number = 9) {
        this.radius = radius;
        this.matrix = map;
        this.maps = createMatrix(
            radius * 2 + 1,
            radius * 2 + 1,
            (x, y) => new Tile(father, x, y, 10), // 这一行其实是无效的
            (x, y) => {
                return new Tile(father, x, y, 10);
            }
        ) as Tile[][];
        this.miniMap = father;
    }
    changeTo(x: number, y: number) {
        // console.log(x, y);
        // this.miniMap.graphics.clear();
        this.maps.forEach((row, Y) => {
            row.forEach((tile, X) => {
                const targetX = X + x - this.radius;
                const targetY = Y + y - this.radius;
                const color = this.matrix?.[targetY]?.[targetX] || 0;
                tile.reprint(color);
            });
        });
        this.maps[this.radius][this.radius].reprint(3);
    }
    _render(player: Laya.Sprite, mapScale: number) {
        this.changeTo(
            Math.round(player.x / mapScale),
            Math.round(player.y / mapScale)
        );
    }
    render(player: Laya.Sprite, mapScale: number) {
        Laya.timer.frameLoop(10, this, this._render, [player, mapScale]);
    }
    stopRender() {
        Laya.timer.clear(this, this._render);
    }
    destroy() {
        this.stopRender();
        this.miniMap = null as any;
        this.matrix = null as any;
        this.maps = null as any;
    }
}
